package com.bridgeintelligent.tag.webserver.dim.mgmt.api.export;

import com.alibaba.excel.annotation.ExcelProperty;
import com.bridgeintelligent.tag.constants.DimCofig;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimData;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author JackCheung
 * @date 2020/7/13 3:30 PM.
 * description:
 */
@Data
public class DimAggData {

    /**
     * 一级分类
     */
    @ExcelProperty(index = 1)
    private String firstCatalog;
    /**
     * 二级分类->catalogId
     */
    @ExcelProperty(index = 2)
    private String secondCatalog;
    /**
     * 存储目录ID
     */
    private String catalogId;
    /**
     * 标签序号，业务说用来排序用？？？ ->sortNum
     */
    private int sortNum;
    /**
     * 标签ID->dimId
     */
    private String dimId;
    /**
     * 标签名称->dimId
     */
    private String dimName;
    /**
     * 标签类型->classify
     */
    private String classify;
    /**
     * 业务含义->definition
     */
    private String definition;
    /**
     * 应用场景->scenarios
     */
    private String scenarios;
    /**
     * 业务规则和业务取值范围，对应数据库字段为产出办法output_method
     * ->
     */
    private String outputMethod;
    /**
     * 展示规则->showType
     */
    private String showType;
    /**
     * 是否现成标签 ->isTagFlag
     */
    private String isTagFlag;
    /**
     * 标签缺省值->defaultValue
     */
    private String defaultValue;
    /**
     * 标签所属机构号->orgNo
     */
    private String orgNo;
    /**
     * 标签来源类型->tagSourceType
     */
    private String tagSourceType;
    /**
     * 更新周期 更新周期:d表示每日更新,m表示每月更新,w表示每周更新->updataCycle
     */
    private String updataCycle;
    /**
     * 标签归属方 = *标签业务联系部门->belongTo
     */
    private String belongTo;
    /**
     * 业务负责人->businessLeader
     */
    private String businessLeader;
    /**
     * 生效日期->
     */
    private String effectiveTime;
    /**
     * 更新日期->
     */
    private String uptTime;
    /**
     * 失效日期->
     */
    private String unEffective;
    /**
     * *来源 *标签归属方 = *标签业务联系部门->businessDepartment
     */
    private String businessDepartment;
    /**
     * 来源系统->dataSource
     */
    private String dataSource;
    /**
     * 数据库英文名->sourcesDb
     */
    private String sourcesDb;
    /**
     * 来源系统表英文->sourcesTable
     */
    private String sourcesTable;
    /**
     * 来源系统字段英文名->sourcesColumn
     */
    private String sourcesColumn;
    /**
     * 数据表客户类型->tagCustType
     */
    private String tagCustType;
    /**
     * 时间周期->timeCycle
     */
    private String timeCycle;
    /**
     * 标签值码值
     */
    private String itemNo;
    /**
     * 标签值
     */
    private String itemName;
    /**
     * 类型，0新增，1更新
     */
    private String type;
    /**
     * 业务类型 01 事实标签、02 预测标签、03 模型标签、04 标签因子
     */
    private String businessType;

    /**
     * 业务所属部门中文名称
     */
    private String belongName;

    public boolean equalsWithoutItem(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        DimAggData that = (DimAggData) o;
//        if (sortNum != that.sortNum) {
//            return false;
//        }
        if (firstCatalog != null ? !firstCatalog.equals(that.firstCatalog) : that.firstCatalog != null) {
            return false;
        }
        if (uptTime != null ? !uptTime.equals(that.uptTime) : that.uptTime != null) {
            return false;
        }
        if (catalogId != null ? !catalogId.equals(that.catalogId) : that.catalogId != null) {
            return false;
        }
        if (dimId != null ? !dimId.equals(that.dimId) : that.dimId != null) {
            return false;
        }
        if (dimName != null ? !dimName.equals(that.dimName) : that.dimName != null) {
            return false;
        }
        if (classify != null ? !classify.equals(that.classify) : that.classify != null) {
            return false;
        }
        if (definition != null ? !definition.equals(that.definition) : that.definition != null) {
            return false;
        }
        if (scenarios != null ? !scenarios.equals(that.scenarios) : that.scenarios != null) {
            return false;
        }
        if (outputMethod != null ? !outputMethod.equals(that.outputMethod) : that.outputMethod != null) {
            return false;
        }
        if (showType != null ? !showType.equals(that.showType) : that.showType != null) {
            return false;
        }
        if (isTagFlag != null ? !isTagFlag.equals(that.isTagFlag) : that.isTagFlag != null) {
            return false;
        }
        if (defaultValue != null ? !defaultValue.equals(that.defaultValue) : that.defaultValue != null) {
            return false;
        }
        if (orgNo != null ? !orgNo.equals(that.orgNo) : that.orgNo != null) {
            return false;
        }
        if (tagSourceType != null ? !tagSourceType.equals(that.tagSourceType) : that.tagSourceType != null) {
            return false;
        }
        if (updataCycle != null ? !updataCycle.equals(that.updataCycle) : that.updataCycle != null) {
            return false;
        }
        if (belongTo != null ? !belongTo.equals(that.belongTo) : that.belongTo != null) {
            return false;
        }
        if (businessLeader != null ? !businessLeader.equals(that.businessLeader) : that.businessLeader != null) {
            return false;
        }
        if (businessDepartment != null ? !businessDepartment.equals(that.businessDepartment) : that.businessDepartment != null) {
            return false;
        }
        if (dataSource != null ? !dataSource.equals(that.dataSource) : that.dataSource != null) {
            return false;
        }
        if (sourcesDb != null ? !sourcesDb.equals(that.sourcesDb) : that.sourcesDb != null) {
            return false;
        }
        if (sourcesTable != null ? !sourcesTable.equals(that.sourcesTable) : that.sourcesTable != null) {
            return false;
        }
        if (sourcesColumn != null ? !sourcesColumn.equals(that.sourcesColumn) : that.sourcesColumn != null) {
            return false;
        }
        if (tagCustType != null ? !tagCustType.equals(that.tagCustType) : that.tagCustType != null) {
            return false;
        }
        if (businessType != null ? !businessType.equals(that.businessType) : that.businessType != null) {
            return false;
        }
        return timeCycle != null ? timeCycle.equals(that.timeCycle) : that.timeCycle == null;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (catalogId != null ? catalogId.hashCode() : 0);
        result = 31 * result + (dimId != null ? dimId.hashCode() : 0);
        result = 31 * result + (dimName != null ? dimName.hashCode() : 0);
        result = 31 * result + (classify != null ? classify.hashCode() : 0);
        result = 31 * result + (definition != null ? definition.hashCode() : 0);
        result = 31 * result + (scenarios != null ? scenarios.hashCode() : 0);
        result = 31 * result + (outputMethod != null ? outputMethod.hashCode() : 0);
        result = 31 * result + (showType != null ? showType.hashCode() : 0);
        result = 31 * result + (isTagFlag != null ? isTagFlag.hashCode() : 0);
        result = 31 * result + (defaultValue != null ? defaultValue.hashCode() : 0);
        result = 31 * result + (orgNo != null ? orgNo.hashCode() : 0);
        result = 31 * result + (tagSourceType != null ? tagSourceType.hashCode() : 0);
        result = 31 * result + (updataCycle != null ? updataCycle.hashCode() : 0);
        result = 31 * result + (belongTo != null ? belongTo.hashCode() : 0);
        result = 31 * result + (businessLeader != null ? businessLeader.hashCode() : 0);
        result = 31 * result + (businessDepartment != null ? businessDepartment.hashCode() : 0);
        result = 31 * result + (dataSource != null ? dataSource.hashCode() : 0);
        result = 31 * result + (sourcesDb != null ? sourcesDb.hashCode() : 0);
        result = 31 * result + (sourcesTable != null ? sourcesTable.hashCode() : 0);
        result = 31 * result + (sourcesColumn != null ? sourcesColumn.hashCode() : 0);
        result = 31 * result + (tagCustType != null ? tagCustType.hashCode() : 0);
        result = 31 * result + (timeCycle != null ? timeCycle.hashCode() : 0);
        result = 31 * result + (itemNo != null ? itemNo.hashCode() : 0);
        result = 31 * result + (itemName != null ? itemName.hashCode() : 0);
        result = 31 * result + (businessType != null ? businessType.hashCode() : 0);
        return result;
    }

    public DimData converToDimData(DimAggData dimAggData) {
        DimData result = new DimData();
        result.setDimLargeType(DimCofig.BASE_DIM);
        result.setCatalogId(dimAggData.getCatalogId());
        result.setSortNum(dimAggData.getSortNum());
        result.setDimId(dimAggData.getDimId());
        result.setDimName(dimAggData.getDimName());
        result.setClassify(dimAggData.getClassify());
        result.setDefinition(dimAggData.getDefinition());
        result.setScenarios(dimAggData.getScenarios());
        result.setOutputMethod(dimAggData.getOutputMethod());
        result.setShowType(dimAggData.getShowType());
        result.setIsTagFlag(dimAggData.getIsTagFlag());
        result.setDefaultValue(dimAggData.getDefaultValue());
        result.setOrgNo(dimAggData.getOrgNo());
        result.setTagSourceType(dimAggData.getTagSourceType());
        result.setUpdataCycle(dimAggData.getUpdataCycle());
        result.setBelongTo(dimAggData.getBelongTo());
        result.setBusinessLeader(dimAggData.getBusinessLeader());
        result.setEffectiveTime(dimAggData.getEffectiveTime());
        if (StringUtils.isNotEmpty(dimAggData.getUptTime())) {
            result.setUptTime(dimAggData.getUptTime());
        } else {
            result.setUptTime(dimAggData.getEffectiveTime());
        }
        result.setBusinessDepartment(dimAggData.getBusinessDepartment());
        result.setDataSource(dimAggData.getDataSource());
        result.setSourcesDb(dimAggData.getSourcesDb());
        result.setSourcesTable(dimAggData.getSourcesTable());
        result.setSourcesColumn(dimAggData.getSourcesColumn());
        result.setTagCustType(dimAggData.getTagCustType());
        result.setTimeCycle(dimAggData.getTimeCycle());
        result.setSystemCode(dimAggData.getDataSource());
        result.setBusinessType(dimAggData.getBusinessType());
        return result;
    }

    private static final DateTimeFormatter YYYY_MM = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    private static String fetchDate() {
        return LocalDateTime.now().format(YYYY_MM);
    }
}